package logic

import (
	"context"
	"novel-start/models/mysql"

	"novel-start/novelrpc/internal/svc"
	"novel-start/novelrpc/novelrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type NovelAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewNovelAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *NovelAddLogic {
	return &NovelAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *NovelAddLogic) NovelAdd(in *novelrpc.NovelAddRequest) (*novelrpc.NovelAddResponse, error) {
	// todo: add your logic here and delete this line

	var user mysql.User

	if err := user.FindUserById(mysql.DB, in.UserId); err != nil {
		return nil, err
	}

	novel := &mysql.Novel{
		Title:     in.Title,
		Type:      in.Type,
		NovelDesc: in.NovelDesc,
		UserId:    in.UserId,
		NovelUrl:  in.NovelUrl,
		NovelNum:  in.NovelNum,
	}

	if err := novel.NovelAdd(mysql.DB); err != nil {
		return nil, err
	}

	return &novelrpc.NovelAddResponse{
		NovelId: int64(novel.ID),
	}, nil
}
